// pages/material/material.js
var app = getApp();
var getSign = require('../../utils/getSign/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: '',
    xieyi:[],
    ishidden: true,
    orderId:0,
    PayType:'企业用户',
    PayName:'',
    PayPhone:'',
    PayNo:'',
    PayEmail:'',
    PayNoPic:[],
    BenefitType:'企业用户',
    BenefitName:'',
    BenefitPhone:'',
    BenefitNo:'',
    BenefitEmail:'',
    BenefitNoPic:[],
    text:'',
    checkboxchange:false
  },
  goTodutys: function (event) {
    var url = event.currentTarget.dataset['url'].replace('?', '$')
    url = url.replace(/=/g, '%')
    url = url.replace(/&/g, '@')
    wx.navigateTo({
      url: '/pages/outweb/outweb?url=' + url,
    })
  },
  //提交材料
  submitcailiao:function(){
    if (this.data.checkboxchange){
      var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;   
      if (!(/^1[34578]\d{9}$/.test(this.data.PayPhone))) {
        wx.showToast({
          title: '手机号码出错',
          duration: 2000
        })
      } else if (!myreg.test(this.data.PayEmail)) {
        wx.showToast({
          title: '邮箱出错',
          duration: 2000
        })
      } else if (!(/^1[34578]\d{9}$/.test(this.data.BenefitPhone))){
        wx.showToast({
          title: '手机号码出错',
          duration: 2000
        })
      }else if (!myreg.test(this.data.BenefitEmail)){
        wx.showToast({
          title: '邮箱出错',
          duration: 2000
        })
      }else{
        var memberInfo = wx.getStorageSync('memberInfo')
        var postArgs = {
          'Api_Type': '017',
          'orderId': this.data.orderId,
          'PayType': this.data.PayType,
          'PayName': this.data.PayName,
          'PayPhone': this.data.PayPhone,
          'PayNo': this.data.PayNo,
          'PayEmail': this.data.PayEmail,
          'PayNoPic': this.data.PayNoPic,
          'BenefitType': this.data.BenefitType,
          'BenefitName': this.data.BenefitName,
          'BenefitPhone': this.data.BenefitPhone,
          'BenefitNo': this.data.BenefitNo,
          'BenefitEmail': this.data.BenefitEmail,
          'BenefitNoPic': this.data.BenefitNoPic,
          'Member_Id': memberInfo.MemberId,
          'Member_Token': memberInfo.MemberToken
        }
        //获取
        getSign.request('https://wx-app-api.sjune.cn/yuren/API.ashx', 'POST', postArgs
          , '').then(
            (res) => {
              console.log(res, '提交材料')
              if (res.Status == 2) {
                wx.clearStorage({
                  success: () => {
                    wx.switchTab({
                      url: '/pages/index/index',
                    })
                  }
                })
              } else if (res.Status == 0) {
                wx.showToast({
                  title: '提交成功',
                  duration: 2000,
                  success: () => {
                    setTimeout(() => {
                      wx.switchTab({
                        url: '/pages/shop/shop',
                      })
                    }, 2000)
                  }
                })
              }
               

            },
            (err) => {
              console.log(err.Message, '提交材料失败')
            }
          )
      }
       
     }else{
      wx.showToast({
        title: '请阅读协议',
        duration: 2000
      })
     }
  },
  changeIsHidden: function () {
    this.setData({
      ishidden: !this.data.ishidden
    })
  },
  textInput:function(e){
    this.setData({
      text: e.detail.value
    })
  },
  checkboxchange:function(e){
    this.setData({
      checkboxchange: !this.data.checkboxchange
    })
    console.log(this.data.checkboxchange)
  },
  radiochange: function (e) {
    this.setData({
      PayType: e.detail.value
    })
  },
  payNameinput:function(e){
    this.setData({
      PayName: e.detail.value
    })
  },
  payPhoneinput:function(e){
    this.setData({
      PayPhone: e.detail.value
    })
  },
  payNoinput:function(e){
    this.setData({
      PayNo: e.detail.value
    })
  },
  payEmailinput:function(e){
    this.setData({
      PayEmail: e.detail.value
    })
  },
  uploadImgs1: function(){
    //上传图片
    var that = this;
    wx.chooseImage({
      count: 4,  //最多可以选择的图片总数
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        console.log("上传服务器前");
        console.log(res);
        // console.log(res.tempFilePaths[0]);
        //启动上传等待中...
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 3000
        })

for (var i = 0; i < res.tempFilePaths.length;i++){
  var path = res.tempFilePaths[i];
  var imgtype = "";
  if (path.indexOf("/") > 0)//如果包含有"/"号 从最后一个"/"号+1的位置开始截取字符串
  {
    imgtype = path.substring(path.lastIndexOf("/") + 1, path.length);
  }
  // that.setData({
  //   Logo: path
  // })
  wx.uploadFile({
    url: 'https://wx-app-api.sjune.cn/yuren/FileUpload.ashx',
    filePath: tempFilePaths[i],
    name: 'FileData',
    formData: {
      'fileName': imgtype
    },
    header: {
      "Content-Type": "multipart/form-data"
    },
    success: function (res) {
      if (res.statusCode == 200) {
        console.log("上传服务器后res.data 购买人");
        console.log(res.data);

        var obj = JSON.parse(res.data); //由JSON字符串转换为JSON对象
        if (obj.status == 1) {
          var pics = that.data.PayNoPic;
          var newpic = new Array(obj.path);
          pics = pics.concat(newpic)
          that.setData({
            PayNoPic: pics
          })
        }
      }
    },
    fail: function (res) {
      wx.hideToast();
      wx.showModal({
        title: '错误提示',
        content: '上传图片失败',
        showCancel: false,
        success: function (res) { }
      })
    }
  });


}

      }
    });
  },

  benefitTypechange: function (e) {
    this.setData({
      BenefitType: e.detail.value
    })
  },
  benefitNameinput: function (e) {
    this.setData({
      BenefitName: e.detail.value
    })
  },
  benefitPhoneinput: function (e) {
    this.setData({
      BenefitPhone: e.detail.value
    })
  },
  benefitNoinput: function (e) {
    this.setData({
      BenefitNo: e.detail.value
    })
  },
  benefitEmailinput: function (e) {
    this.setData({
      BenefitEmail: e.detail.value
    })
  },
  uploadImgs2: function () {
    //上传图片
    var that = this;
    wx.chooseImage({
      count: 4,  //最多可以选择的图片总数
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        console.log("上传服务器前");
        console.log(res);
        // console.log(res.tempFilePaths[0]);
        //启动上传等待中...
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 3000
        })

        for (var i = 0; i < res.tempFilePaths.length; i++) {
          var path = res.tempFilePaths[i];
          var imgtype = "";
          if (path.indexOf("/") > 0)//如果包含有"/"号 从最后一个"/"号+1的位置开始截取字符串
          {
            imgtype = path.substring(path.lastIndexOf("/") + 1, path.length);
          }
          // that.setData({
          //   Logo: path
          // })
          wx.uploadFile({
            url: 'https://wx-app-api.sjune.cn/yuren/FileUpload.ashx',
            filePath: tempFilePaths[i],
            name: 'FileData',
            formData: {
              'fileName': imgtype
            },
            header: {
              "Content-Type": "multipart/form-data"
            },
            success: function (res) {
              if (res.statusCode == 200) {
                console.log("上传服务器后res.data 购买人");
                console.log(res.data);

                var obj = JSON.parse(res.data); //由JSON字符串转换为JSON对象
                if (obj.status == 1) {
                  var pics = that.data.BenefitNoPic;
                  var newpic = new Array(obj.path);
                  pics = pics.concat(newpic)
                  that.setData({
                    BenefitNoPic: pics
                  })
                  // console.log(obj.path)
                }
              }
            },
            fail: function (res) {
              wx.hideToast();
              wx.showModal({
                title: '错误提示',
                content: '上传图片失败',
                showCancel: false,
                success: function (res) { }
              })
            }
          });


        }

      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //options.id
    console.log(options.id)
    this.setData({
      baseUrl: app.baseUrl,
      orderId: options.id
    })

    var postArgs = {
      'Api_Type': '025',
      'page': 2
    }
    //获取
    getSign.request('https://wx-app-api.sjune.cn/yuren/API.ashx', 'POST', postArgs, '').then(
      (res) => {
        console.log(res, '协议')
        if (res.Status == 2) {
          wx.clearStorage({
            success: () => {
              wx.switchTab({
                url: '/pages/index/index',
              })
            }
          })
        } else if (res.Status == 0) {
          this.setData({
            xieyi: res.Data,
          })
        }
        
      },
      (err) => {
        console.log(err.Message, '协议失败')
      }
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})